package com.itheima.reggie.web.controller;

import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//点餐端地址管理的表现层控制器
@RestController
@RequestMapping("/address")
public class AddressController {

    @Autowired
    private AddressService addressService;

    //查询地址列表(登录用户的地址信息)
    @GetMapping("/list")
    public ResultInfo findList() {
        //调用service
        List<Address> addressList = addressService.findList();
        //返回
        return ResultInfo.success(addressList);
    }

    //新增地址
    @PostMapping
    public ResultInfo save(@RequestBody Address address) {
        //调用service
        addressService.save(address);
        //返回
        return ResultInfo.success();
    }

    //地址回显
    @GetMapping("/{id}")
    public ResultInfo findById(@PathVariable Long id) {
        //调用service
        Address address = addressService.findById(id);
        //返回
        return ResultInfo.success(address);
    }

    //修改地址
    @PutMapping
    public ResultInfo update(@RequestBody Address address) {
        //调用service
        addressService.update(address);
        //返回
        return ResultInfo.success();
    }

    //删除地址
    @DeleteMapping
    public ResultInfo delete(@RequestParam List<Long> ids) {
        //调用service
        addressService.delete(ids);
        //返回
        return ResultInfo.success();
    }

    //修改默认地址
    @PutMapping("/default")
    public ResultInfo updateDefault(@RequestBody Address address) {
        //调用service
        addressService.updateDefault(address);
        //返回
        return ResultInfo.success();
    }

    //查询默认地址
    @GetMapping("/default")
    public ResultInfo getDefaultAddress() {
        //调用service
        Address address = addressService.findDefaultAddress();
        //判断是否有默认地址
        if (address == null) {
            return ResultInfo.error("没有发现默认地址!");
        }
        //返回
        return ResultInfo.success(address);
    }

}
